package com.top10.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.top10.common.Const;
import com.top10.entity.GoodsDetailed;
import com.top10.entity.SpecialPrice;
import com.top10.entity.UserCart;
import com.top10.entity.vo.SkuVo;
import com.top10.entity.vo.UserCartVo;
import com.top10.mapper.UserCartMapper;
import com.top10.service.IGoodsDetailedService;
import com.top10.service.ISpecialPriceService;
import com.top10.service.IUserCartService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ellen
 * @since 2018-10-07
 */
@Service
public class UserCartServiceImpl extends ServiceImpl<UserCartMapper, UserCart> implements IUserCartService {

    @Autowired
    UserCartMapper userCartMapper;
    @Autowired
    private ISpecialPriceService iSpecialPriceService;
    @Autowired
    private IGoodsDetailedService iGoodsDetailedService;

    @Override
    public List cartList(String userUuid) {
        Gson gson = new GsonBuilder().serializeNulls()
                .setPrettyPrinting()
                .disableHtmlEscaping()
                .create();

        /*List<UserCartVo> userCartVos=new ArrayList<>();*/
        List<UserCartVo> userCartVos = userCartMapper.cartList(userUuid);

        for(UserCartVo userCartVo:userCartVos){
            List<SpecialPrice> speciaGood=iSpecialPriceService.selectList(new EntityWrapper<SpecialPrice>().eq("isdeleted", Const.SpecialIsNotDeleted)
                    .eq("goods_uuid",userCartVo.getGuuid()).gt("end_time",new Date()));
            if(speciaGood.size()!=0){
            SpecialPrice specialPrice=iSpecialPriceService.selectOne(new EntityWrapper<SpecialPrice>()
                    .eq("type",1)
                    .eq("isdeleted", Const.SpecialIsNotDeleted)
                    .eq("goods_uuid", userCartVo.getGuuid())
                    .gt("end_time",new Date())
                    .ge("limited_number",userCartVo.getQuantity())
                    .le("min_number",userCartVo.getQuantity()));
            if(specialPrice!=null) {
                String[] line =userCartVo.getSkukey().split(",");
                GoodsDetailed goodsDetailed=iGoodsDetailedService.selectOne(new EntityWrapper<GoodsDetailed>().eq("goods_uuid",userCartVo.getGuuid()));

                if(goodsDetailed.getSku()!=null){
                    String sku= goodsDetailed.getSku();
                    List<SkuVo> skuvo=gson.fromJson(sku, new TypeToken<List<SkuVo>>(){}.getType());
                    for (int i=0;i<=skuvo.size()-1;i++){
                        int c=-1;
                        for(int j=0;j<=line.length-1;j++){
                            if(skuvo.get(i).getKeyin().contains(line[j])){
                                c++;
                                if(c==line.length-1){
                                    userCartVo.setPrice(skuvo.get(i).getSkuprice());
                                    /*  userCartVos.add(userCartVo);*/
                                }
                            }
                        }
                    }
                }
               /* userCartVos.add(userCartVo);*/
            }
                SpecialPrice specialPrice1=iSpecialPriceService.selectOne(new EntityWrapper<SpecialPrice>()
                        .eq("type",2)
                        .eq("isdeleted", Const.SpecialIsNotDeleted)
                        .eq("goods_uuid", userCartVo.getGuuid())
                        .gt("end_time",new Date()));
                if(specialPrice1!=null){
                    String[] line =userCartVo.getSkukey().split(",");
                    GoodsDetailed goodsDetailed=iGoodsDetailedService.selectOne(new EntityWrapper<GoodsDetailed>().eq("goods_uuid",userCartVo.getGuuid()));

                    if(goodsDetailed.getSku()!=null){
                        String sku= goodsDetailed.getSku();
                        List<SkuVo> skuvo=gson.fromJson(sku, new TypeToken<List<SkuVo>>(){}.getType());
                        for (int i=0;i<=skuvo.size()-1;i++){
                            int c=-1;
                            for(int j=0;j<=line.length-1;j++){
                                if(skuvo.get(i).getKeyin().contains(line[j])){
                                    c++;
                                    if(c==line.length-1){
                                        userCartVo.setPrice(skuvo.get(i).getSkuprice());
                                        /*  userCartVos.add(userCartVo);*/
                                    }
                                }
                            }
                        }
                    }
                 /*   userCartVos.add(userCartVo);*/
                }
                if(specialPrice==null && specialPrice1==null){
                    String[] line =userCartVo.getSkukey().split(",");
                    GoodsDetailed goodsDetailed=iGoodsDetailedService.selectOne(new EntityWrapper<GoodsDetailed>().eq("goods_uuid",userCartVo.getGuuid()));

                    if(goodsDetailed.getSku()!=null){
                        String sku= goodsDetailed.getSku();
                        List<SkuVo> skuvo=gson.fromJson(sku, new TypeToken<List<SkuVo>>(){}.getType());
                        for (int i=0;i<=skuvo.size()-1;i++){
                            int c=-1;
                            for(int j=0;j<=line.length-1;j++){
                                if(skuvo.get(i).getKeyin().contains(line[j])){
                                    c++;
                                    if(c==line.length-1){
                                        userCartVo.setPrice(skuvo.get(i).getSkuprice());
                                        /*  userCartVos.add(userCartVo);*/
                                    }
                                }
                            }
                        }
                    }
                }


            }else{
                String[] line =userCartVo.getSkukey().split(",");
                GoodsDetailed goodsDetailed=iGoodsDetailedService.selectOne(new EntityWrapper<GoodsDetailed>().eq("goods_uuid",userCartVo.getGuuid()));

                if(goodsDetailed.getSku()!=null){
                    String sku= goodsDetailed.getSku();
                    List<SkuVo> skuvo=gson.fromJson(sku, new TypeToken<List<SkuVo>>(){}.getType());
                    for (int i=0;i<=skuvo.size()-1;i++){
                        int c=-1;
                        for(int j=0;j<=line.length-1;j++){
                            if(skuvo.get(i).getKeyin().contains(line[j])){
                                c++;
                                if(c==line.length-1){
                                    userCartVo.setPrice(skuvo.get(i).getSkuprice());
                                  /*  userCartVos.add(userCartVo);*/
                                }
                            }
                        }
                    }
                }
            }
        }


        //记录相同商品下标
        Set<Integer> sameSet = new HashSet<>();
        for (int i = 0; i < userCartVos.size() - 1; i++) {
            for (int j = i + 1; j < userCartVos.size(); j++) {
                Integer gid1 = userCartVos.get(i).getGid();
                Integer gid2 = userCartVos.get(j).getGid();
                String skukey1 = userCartVos.get(i).getSkukey();
                String skukey2 = userCartVos.get(j).getSkukey();
                if (gid1.equals(gid2) && skukey1.equals(skukey2)) {
                    sameSet.add(i);
                    sameSet.add(j);
                }
            }
        }

        if(sameSet.size() > 0){
            //记录相同商品总数量
            Integer total = 0;
            for (Integer i : sameSet) {
                Integer quantity = userCartVos.get(i).getQuantity();
                total += quantity;
            }

            //去除重复商品
            int index = 0;
            for(Integer i : sameSet){
                if(index == 0){
                    userCartVos.get(i).setQuantity(total);
                }else {
                    userCartVos.remove((int)i-index+1);
                }
                index++;
            }
        }

        Set<String> uuidSet = new HashSet<>(); //用于记录店铺uuid
        for (UserCartVo userCartVo : userCartVos) {
            uuidSet.add(userCartVo.getSuuid());
        }

        List<List<UserCartVo>> shopList = new ArrayList<>();
        for (String uuid : uuidSet) {
            List<UserCartVo> userCartList = new ArrayList<>();
            for (UserCartVo userCartVo : userCartVos) {
                if (uuid.equals(userCartVo.getSuuid())) {
                    userCartList.add(userCartVo);
                }
            }
            shopList.add(userCartList);
        }

        return shopList;
    }
}

            /*    if(goodsDetailed.getSku()!=null){
                    String sku= goodsDetailed.getSku();
                    List<SkuVo> skuvo=gson.fromJson(sku, new TypeToken<List<SkuVo>>(){}.getType());
                    for (int i=0;i<skuvo.size();i++){
                        if(skuvo.get(i).getKeyin().contains(userCartVo.getSkukey())){
                          userCartVo.setPrice(skuvo.get(i).getSkuprice());
                          userCartVos.add(userCartVo);
                          break;
                        }
                    }
                }*/